// 解析url中的查询字符串
function decodeQuery(){
    var search = decodeURI(document.location.search);
    return search.replace(/(^\?)/, '').split('&').reduce(function(result, item){
        values = item.split('=');
        result[values[0]] = values[1];
        return result;
    }, {});
}

$(function(){
    // 页面加载完毕，获取新闻列表
    getNewsList(1);

    // 关注当前作者
    $(".focus").click(function () {
        var user_id = $(this).attr("data-userid");
        var params = {
            "action": "follow",
            "user_id": user_id
        }

        $.ajax({
            url: "/news/followed_user",
            type: "post",
            contentType: "application/json",
            headers: {"X-CSRFToken": getCookie("csrf_token")},
            data: JSON.stringify(params),
            success: function (resp) {
                if (resp.errno == "0") {
                    $(".focus").hide();
                    $(".focused").show();
                }else if (resp.errno == "4101"){
                    $(".login_form_con").show();
                }else {
                    alert(resp.errmsg)
                }
            }
        })
    });

    // 取消关注当前作者
    $(".focused").click(function () {
        var user_id = $(this).attr("data-userid");
        var params = {
            "action": "unfollow",
            "user_id": user_id
        };

        $.ajax({
            url: "/news/followed_user",
            type: "post",
            contentType: "application/json",
            headers: {"X-CSRFToken": getCookie("csrf_token")},
            data: JSON.stringify(params),
            success: function (resp) {
                if (resp.errno == "0") {
                    $(".focus").show();
                    $(".focused").hide();
                }else if (resp.errno == "4101"){
                    $(".login_form_con").show();
                }else {
                    alert(resp.errmsg)
                }
            }
        })
    })
});

// TODO 获取新闻列表
function getNewsList(page) {

}
